package com.hyc.myvideo.ui.activity.home;

import android.view.View;
import android.widget.AdapterView;
import butterknife.BindView;
import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.TrainingCatalogAdapter;
import com.hyc.myvideo.data.api.constants.Constants;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Practice;
import com.hyc.myvideo.data.api.model.PracticeModel;
import com.hyc.myvideo.data.api.model.PracticeRecord;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * 修炼目录  添加课程
 * Created by dfeads on 2017/7/22.
 */

public class TrainingCatalogSecActivity extends BaseFActivity {

  @BindView(R.id.activity_audio_list_refresh) PullToRefreshListView mPullRefresh;
  private TrainingCatalogAdapter trainingCatalogAdapter;
  private List<Practice> practiceList;

  private String curriculum_id = "";

  @Override public int getContentViewId() {
    return R.layout.activity_audio_list;
  }

  @Override protected void initView() {

    curriculum_id = getIntent().getStringExtra("id");

    practiceList = new ArrayList<>();
    trainingCatalogAdapter =
        new TrainingCatalogAdapter(TrainingCatalogSecActivity.this, practiceList);
    mPullRefresh.setAdapter(trainingCatalogAdapter);

    setLeftBtnClick(true, "修炼目录");
    setRightClick(R.mipmap.training_add, new View.OnClickListener() {
      @Override public void onClick(View view) {

      }
    });

    mPullRefresh.setMode(PullToRefreshBase.Mode.DISABLED);
    mPullRefresh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        Practice practice = practiceList.get(position-1);
        if (practice != null) {
          api.setPracticeCatelogCurriculum(userInfo.getUser_id(), practice.getCatelog_id(), curriculum_id);
        }
      }
    });
  }

  @Override protected void initData() {
    api.getPracticeList(userInfo.getUser_id());
  }

  @Override protected void oNext(String resulte, String method) {
    if (method.equals(Constants.API_PRACTICE)) {
      BaseResult<PracticeModel> practiceModel =
          JSONObject.parseObject(resulte, new TypeReference<BaseResult<PracticeModel>>() {
          });
      if (practiceModel.isCode()) {
        practiceList.clear();
        practiceList.addAll(practiceModel.getResult().getList());
        trainingCatalogAdapter.updateListView(practiceList);
      } else {
        Toaster.show(practiceModel.getMsg());
      }
    } else if (method.equals(Constants.API_PRACTICE_SET_CATELOG_CURRICULUM)) {
      BaseResult<String> base =
          JSONObject.parseObject(resulte, new TypeReference<BaseResult<String>>() {
          });
      Toaster.show(base.getMsg());
    }
  }

  @Override protected void oError() {

  }
}
